
import 'package:flutter_module/map/models/library_models.dart';
import 'package:geocore/geocore.dart';
import 'package:geobase/src/utils/formats.dart';

class GeometryTool {
  static String getPointWkt(NIGeoPoint geoPoint) {
    return "POINT(${geoPoint.longitude} ${geoPoint.latitude})";
  }

  static String getPolygonWkt(List<NIGeoPoint> geoPoints) {

    if(geoPoints.isNotEmpty){

      List<GeoPoint2> points = [];

      for (var geo in geoPoints) {
        points.add(GeoPoint2(lon: geo.longitude, lat: geo.latitude));
      }

      //增加结束点闭合业务
      if (points.length > 3 && points[0] != points[points.length - 1]) {
         points.add(points[0]);
      }

      List<List<GeoPoint2>> rings = [];
      rings.add(points);


      return Polygon.fromPoints(rings).toStringAs(format: const WktFormat());
    }

    return "";
  }

  static String getLineWkt(List<NIGeoPoint> geoPoints) {

    if(geoPoints.isNotEmpty){

      List<GeoPoint> points = [];

      for (var geo in geoPoints) {
        points.add(GeoPoint2(lon: geo.longitude, lat: geo.latitude));
      }

      return LineString.any(PointSeries<GeoPoint>.from(points)).toStringAs(format: const WktFormat());

    }

    return "";
  }

  static NIGeoPoint? getGeoPintFromWkt(String wkt) {
    var geometry = wktGeographic.parse(wkt);
    if (geometry is GeoPoint) {
      return NIGeoPoint(latitude: geometry.lat, longitude: geometry.lon);
    }
  }
}
